<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    console.log(React)
    class App extends React.Component{
        // 创建一个Ref对象
        h1Ref=React.createRef();
        h2Ref=React.createRef();
        childRef = React.createRef();
        state={

        }
        render(){
            console.log(this.h1Ref)
            return (
                <div>
                    <h1 ref={this.h1Ref}>哈哈哈哈哈哈哈</h1>  
                    <h2 ref={this.h2Ref}>嘿嘿嘿嘿</h2>  
                    <button onClick={()=>{
                        // 获取 dom对象 this.属性名.current
                        console.log(this.h1Ref.current)
                        this.h1Ref.current.style.color='red'
                        console.log(this.h2Ref.current)
                    }}>获取h1</button>
                    <Child ref={this.childRef}/>
                    <button onClick={()=>{
                        this.childRef.current.changeMsg();
                    }}>获取组件实例</button>
                </div>
            )
        }
    }
    class Child extends React.Component{
        state={
            msg:"好好学习天天游戏"
        }
        render(){
             return(
                <div>
                    <h3>{this.state.msg}</h3>    
                </div>
             )
        }
        changeMsg(){
            this.setState({
                msg:"学习使我快乐"
            })
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>